
//
//  YDQuestionTableHeaderView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/13.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDQuestionTableHeaderView.h"
#import "YDQuestionHeaderModel.h"

@interface YDQuestionTableHeaderView()
//图片
@property (nonatomic , weak) UIImageView *titleImage;
//内容
@property (nonatomic , weak) UILabel *contentLabel;
//按钮
@property (nonatomic , weak) UIButton *contentButton;
//分割线
@property (nonatomic , weak) UIView *lineView;
@end

@implementation YDQuestionTableHeaderView


-(void)setModel:(YDQuestionHeaderModel *)model
{
    _model = model;
    
    self.contentLabel.text = model.title;
    
    self.titleImage.frame = model.imageFrame;
    
    self.contentLabel.frame = model.titleFrame;
    
    self.contentButton.selected = model.opened;
    
    
    self.contentButton.frame = model.buttonFrame;
    
    self.lineView.frame = model.lineFrame;
}

+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    
    static NSString *questionTableHeaderView = @"YDQuestionTableHeaderView";
    YDQuestionTableHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:questionTableHeaderView];
    if (headerView == nil) {
        headerView = [[YDQuestionTableHeaderView alloc] init];
    }
    return headerView;
}


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self titleImage];
        
        [self contentLabel];
        [self contentButton];
        
        [self lineView];
        
        
    }
    return self;
}


-(UIImageView *)titleImage
{
    if (_titleImage == nil) {
        UIImageView *titleImage = [[UIImageView alloc] init];
        [self.contentView addSubview:titleImage];
        titleImage.contentMode = UIViewContentModeScaleAspectFill;
        titleImage.image = [UIImage imageNamed:@"home-wen"];
        _titleImage = titleImage;
    }
    return _titleImage;
}

-(UILabel *)contentLabel
{
    if (_contentLabel == nil) {
        UILabel *contentLabel = [[UILabel alloc] init];
        [self.contentView addSubview:contentLabel];
        contentLabel.textColor = [UIColor colorWithHexString:dark];
        contentLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.numberOfLines = 0;
        _contentLabel = contentLabel;
    }
    return _contentLabel;
}
-(UIButton *)contentButton
{
    if (_contentButton == nil) {
        UIButton *contentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [contentButton addTarget:self action:@selector(contentButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentButton setImage:[UIImage imageNamed:@"home-zhankai"] forState:UIControlStateNormal];
        [contentButton setImage:[UIImage imageNamed:@"home-shouqi"] forState:UIControlStateSelected];
        contentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        contentButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, rate375(20));//按钮内容的对齐方式
        contentButton.imageView.contentMode = UIViewContentModeCenter;//按钮的图片模式
        contentButton.imageView.clipsToBounds = NO;
        [self.contentView addSubview:contentButton];
//        [contentButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.bottom.top.equalTo(self);
//        }];
        _contentButton = contentButton;
    }
    return _contentButton;
}

-(void)contentButtonClick:(UIButton *)button
{
    button.selected = !button.selected;
    
    self.model.opened = !self.model.isOpened;
    
    if ([self.delegate respondsToSelector:@selector(questionTableHeaderViewContentButtonClick:)]) {
        [self.delegate questionTableHeaderViewContentButtonClick:self];
    }
    
    
}

-(UIView *)lineView
{
    if (_lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        [self.contentView addSubview:lineView];
        lineView.backgroundColor = NumberColor(247, 247, 247);
//        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self);
//            make.height.equalTo(@1);
//            make.left.equalTo(self).offset(rate375(20));
//            make.right.equalTo(self).offset(-rate375(20));
//        }];
        
        _lineView = lineView;
    }
    return _lineView;
}
@end
